﻿#include "BrowserTableHandlerSubsystemBeam.h"
#include "OperatorsInterface/OperAbstractSubsystem.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysBeam.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysGeoProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyBeam.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

namespace GUI
{
    void BrowserTableHandlerSubsystemBeam::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Physical Properties") << tr("Flexural-X DLF")
            << tr("Flexural-Y DLF") << tr("Length[m]") << tr("Mass[kg]")<< tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerSubsystemBeam::updateData()
    {
        if (_tableWidget == nullptr) return;
        if (_seaMgr == nullptr || _propMgr == nullptr )return;

        QList<Acoustics::FITKAbstractSEASubSys*> list = _seaMgr->getSubSysByType(
            Acoustics::FITKAbstractSEASubSys::SEABeam);

        //物理属性数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换为SEA梁
            Acoustics::FITKSEASubSysBeam* sea = dynamic_cast<Acoustics::FITKSEASubSysBeam*>(list.at(i));
            if (sea == nullptr) continue;

            //获取几何属性
            Acoustics::FITKSEASubSysGeoProp* geoProp = sea->getGeoProp();
            if (!geoProp)continue;
            double length = geoProp->getGeoProp(Acoustics::FITKSEASubSysGeoProp::GeoPropType::Circumference);

            auto  phyProp = _propMgr->getDataByID(sea->getPropSectionID());
            if (!phyProp)continue;
            Acoustics::FITKPhyscisPropertyBeam* beamPhyPropData = dynamic_cast<Acoustics::FITKPhyscisPropertyBeam*>(phyProp);
            if (!beamPhyPropData) continue;
            Acoustics::FITKAcousticsAbsMaterial* materialData = beamPhyPropData->getMaterial();
            double area = beamPhyPropData->getCrossSectionArea() * length;
            double mass = area * materialData->getDensity();

            //数据ID
            const int id = sea->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(sea->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(phyProp->getDataObjectName(), id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(sea->getFlexureXSpectra()->getDataObjectName(), id));
            _tableWidget->setItem(i, 3, new QTableWidgetItem(sea->getFlexureYSpectra()->getDataObjectName(), id));
            _tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(length), id));
            _tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(mass), id));
            //_tableWidget->setItem(i, 6, new QTableWidgetItem(sea->getDescribe(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractSubsystem * BrowserTableHandlerSubsystemBeam::getSubsystemOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractSubsystem>("OperSubsystemBeam");
    }

    bool BrowserTableHandlerSubsystemBeam::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //第2到4列能修改
        if (colIndex < 1 || colIndex>3) return false;

        return true;
    }



}